#include "charging_zhongsheng_io.h"
#include "utility.h"
chargingZhongSheng_IO::chargingZhongSheng_IO( QObject* parent ) {}

chargingZhongSheng_IO::~chargingZhongSheng_IO() {}

bool chargingZhongSheng_IO::setOutput( QTcpSocket& mSocket, ChargingTcpDrive::Operator oper ) {
    if ( !isConnected( mSocket ) ) { return false; }
    qDebug() << "chargingZhongSheng_IO"
             << "Operator: " << oper;
    uint8_t data[ 13 ];
    switch ( oper ) {
    case OPEN:
        //        data = open_data;
        memcpy( data, open_data, sizeof( uint8_t ) * 13 );
        if ( limits.front ) {
            qDebug() << "移动到位！";
            memcpy( data, close_all_data, sizeof( uint8_t ) * 13 );
        }
        break;
    case CLOSE:
        //        data = close_data;
        memcpy( data, close_data, sizeof( uint8_t ) * 13 );
        if ( limits.back ) {
            qDebug() << "移动到位！";
            memcpy( data, close_all_data, sizeof( uint8_t ) * 13 );
        }
        break;
    case STOP:
        qDebug() << "中盛科技 无停止，调关闭";
        memcpy( data, close_all_data, sizeof( uint8_t ) * 13 );

        break;
    case UNKNOWN:
        return false;
    }
    //    qDebug() << data;
    QByteArray array( ( char* )data, 13 );

    qDebug() << array << QString( "Send: [%1:%2]  " ).arg( mSocket.peerName() ).arg( mSocket.peerPort() );

    emit sigShowTCPData( array, QString( "Send: [%1:%2] [写输出] " ).arg( mSocket.peerName() ).arg( mSocket.peerPort() ) );
    mSocket.write( array );

    if ( mSocket.waitForReadyRead( 1000 ) ) {
        QByteArray feedback = mSocket.readAll();
        qDebug() << "充电桩控制返回" << feedback;
        sigShowTCPData( feedback, QString( "Recv: [%1:%2] [写输出] " ).arg( mSocket.peerName() ).arg( mSocket.peerPort() ) );

        if ( feedback.size() < 8 ) { return false; }

        return true;
    }
    else {
        return false;
    }
}

bool chargingZhongSheng_IO::updateInput( QTcpSocket& mSocket ) {
    qDebug() << &mSocket;
    if ( !isConnected( mSocket ) ) { return false; }

    mSocket.readAll();

    QByteArray array( ( char* )state_in, 8 );
    qDebug() << array << QString( "Send: [%1:%2]  " ).arg( mSocket.peerName() ).arg( mSocket.peerPort() );

    emit sigShowTCPData( array, QString( "Send: [%1:%2] [读输入] " ).arg( mSocket.peerName() ).arg( mSocket.peerPort() ) );
    mSocket.write( array );

    if ( mSocket.waitForReadyRead( 1000 ) ) {
        QByteArray feedback = mSocket.readAll();
        qDebug() << feedback << QString( "Recv: [%1:%2]  " ).arg( mSocket.peerName() ).arg( mSocket.peerPort() );
        sigShowTCPData( feedback, QString( "Recv: [%1:%2] [读输入] " ).arg( mSocket.peerName() ).arg( mSocket.peerPort() ) );

        if ( feedback.size() < 13 ) {
            // 01 04 0B 00 01 00 00 00 00 00 00 00 00 输入通道1，2, 3, 4
            qDebug() << "ChargingPile::updateInput: size of feedback < 13, " << Utility::QbyteArry2Qstring( feedback );
            return false;
        }
        limits.front = feedback.at( 4 ) == 1;
        limits.back  = feedback.at( 6 ) == 1;
        qDebug() << "前限位感应" << limits.front;
        qDebug() << "后限位感应" << limits.back;
        return limits.back || limits.front;
    }
}

bool chargingZhongSheng_IO::updateOutput( QTcpSocket& mSocket ) {

    if ( !isConnected( mSocket ) ) { return false; }
    mSocket.readAll();
    qDebug() << &mSocket;
    QByteArray send( ( char* )state_out, 8 );

    emit sigShowTCPData( send, QString( "Send: [%1:%2]  " ).arg( mSocket.peerName() ).arg( mSocket.peerPort() ) );
    mSocket.write( send );
    if ( mSocket.waitForReadyRead( 1000 ) ) {
        QByteArray feedback = mSocket.readAll();
        sigShowTCPData( feedback, QString( "Recv: [%1:%2]  " ).arg( mSocket.peerName() ).arg( mSocket.peerPort() ) );
        if ( feedback.size() < 13 ) {
            // 01 04 0B 00 01 00 00 00 00 00 00 00 00 输入通道1，2, 3, 4
            qDebug() << "ChargingPile::updateOutput: size of feedback < 13, " << Utility::QbyteArry2Qstring( feedback );
            return false;
        }
        output = UNKNOWN;
        if ( feedback.at( 4 ) == 0x01 ) { output = OPEN; }
        if ( feedback.at( 6 ) == 0x01 ) { output = CLOSE; }
        if ( feedback.at( 4 ) == 0x00 && feedback.at( 6 ) == 0x00 ) { output = STOP; }

        return true;
    }
}
